var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();

Page({
  data: {
    bannerInfo: {
      'img_url': '',
      'name': ''
    },
    categoryFilter: false,
    filterCategory: [],
    goodsList: [],
    categoryId: 0,
    categoryName:'全部',
    currentSortType: 'default',
    currentSortOrder: 'desc',
    page: 1,
    size: 10,
    queryType:'newGoods'
  },
  //请求新品品牌信息
  getData: function () {
    let that = this;
    let url = that.data.queryType == 'hotGoods' ? api.GoodsHot : api.GoodsNew;
    util.request(url).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          bannerInfo: res.data.bannerInfo,
        });
        that.getGoodsList();
      }
    });
  },
  //请求商品列表 
  getGoodsList() {
    var that = this;
    return new Promise(function (resolve, reject) {
      util.request(api.GoodsList, {
        isNew: (that.data.queryType == 'newGoods' ? 1 : ''),
        isHot: (that.data.queryType == 'hotGoods' ? 1 : ''),
        page: that.data.page,
        size: that.data.size,
        order: that.data.currentSortOrder,
        sort: that.data.currentSortType,
        categoryId: that.data.categoryId
      }).then(function (res) {
        if (res.errno === 0) {
          that.setData({
            goodsList: that.data.goodsList.concat(res.data.goodsList),
            filterCategory: res.data.filterCategory
          });
          res.data.filterCategory.filter(function (v) {
            if (v.checked == true) {
              that.setData({
                categoryName: v.name
              });
            }
          });
          resolve(res.data);
        }
      });
    });
  },
  //页面初始化 options为页面跳转所带来的参数
  onLoad: function (options) {
    var that = this;
    that.setData({
      queryType: options.queryType,
    });
    this.getData();
  },
  //筛选条件
  openSortFilter: function (event) {
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'categoryFilter':
        this.setData({
          categoryFilter: !this.data.categoryFilter,
          currentSortType: 'category',
          currentSortOrder: 'asc'
        });
        break;
      case 'priceSort':
        let tmpSortOrder = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmpSortOrder = 'desc';
        }
        this.setData({
          currentSortType: 'price',
          currentSortOrder: tmpSortOrder,
          categoryFilter: false,
          page: 1,
          goodsList: [],
        });
        this.getGoodsList();
        break;
      default:
        //综合排序
        this.setData({
          currentSortType: 'default',
          currentSortOrder: 'desc',
          categoryFilter: false,
          page: 1,
          goodsList: [],
        });
        this.getGoodsList();
    }
  },
  //选择分类
  selectCategory: function (event) {
    let currentId = event.target.dataset.categoryId;
    this.setData({
      categoryFilter: false,
      categoryId: currentId,
      page: 1,
      goodsList: [],
    });
    this.getGoodsList();
  },
  //增加下拉刷新数据的功能
  onPullDownRefresh() {
    console.log("下拉刷新数据");
    var that = this;
    that.setData({
      page: 1,
      goodsList: [],
    });
    that.getGoodsList();
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    console.log("下一页")
    var that = this;
    that.setData({
      page: that.data.page + 1,
    });
    that.getGoodsList();
    return;

    var page = that.data.page;
    var size = that.data.size;
    var predictAmount = page * size;
    var realityAmount = that.data.goodsList.length;
    if (realityAmount == predictAmount){
      that.setData({
        page: that.data.page + 1,
      });
      that.getGoodsList();
      return;
    };
    var previousAmount = (page - 1) * size;
    if (previousAmount <= realityAmount < predictAmount) {
      that.setData({
        page: 1,
        size: predictAmount,
        goodsList:[]
      });
      that.getGoodsList().then(function (res) {
        that.setData({
          page: page,
          size: size
        });
      });
    }
  },
})